<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Jobs extends Controller
{
    public function _initialize()
    {
        if(!Session::has('Adminname')){
            $this->error('请登录!','../admin');
        }
    }
    public function index()
    {
        if(Session::get('Adminpower')==1){
            $list = Db::name('jobs')->order('id','asc')->cache(true,10)->paginate(10);
        }else{
    	   $map['company'] = Session::get('AdminCompany'); 
    	   $list = Db::name('jobs')->where($map)->order('id','asc')->cache(true,10)->paginate(10); 
        }
    	$page = $list->render();
    	$this->assign('list', $list);
		$this->assign('page', $page);
        return $this->fetch();
    }

    //新增工作
    public function add()
    {
        $jobs = model('Jobs');
        $param = request()->param();
        // return json(['message'=>$param]);
        $param['company'] = Session::get('AdminCompany');
        $data = $jobs->addjobs($param);
        if($data){
            $this->success('创建成功!');
            // return json(['message'=>'创建成功!']);
        }
        return json(['message'=>$jobs->getError()],400);
    }

    //查看投递详情
    public function visit()
    {
        $id =  request()->get('id');
        $map['jobid'] = $id;
        $list = Db::name('jobjoin')->where($map)->cache(true,10)->paginate(10);
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch('detail/resumelist');
    }

   	//删除工作
    public function delete()
    {
    	$jobs = model('Jobs');
    	$param = request()->get('id');
    	$data = $jobs -> deletejobs($param);
    	if($data){
    		return json(['message'=>'删除成功!']);
    	}
    	return json(['message'=>$jobs->getError()]);
    }

        //更新页面
    public function updatedetail()
    {
        $jobs = model('Jobs');
        $param = request()->get('id');
        $data = $jobs::get($param);
        $this->assign('list',$data);
        return $this->fetch('detail/jobs');
    }

    //更新
    public function update()
    {
    	$jobs = model('Jobs');
    	$param = request()->param();
    	$data = $jobs -> updatejobs($param);
    	if($data){
            $this->success('更新成功!');
            // return json(['message'=>'更新成功!']);
        }
        return json(['message'=>$jobs->getError()]);
    }

    //取消发布
    public function cancel()
    {
    	$jobs = model('Jobs');
    	$param = request()->get('id');
    	$data=$jobs->canceljobs($param);
    	if($data){
    		return json(['message'=>'取消发布成功!']);
    	}
    	return json(['message'=>$jobs->getError()]);
    }

    //发布
    public function fabu()
    {
    	$jobs = model('Jobs');
    	$param = request()->get('id');
    	$data=$jobs->fabujobs($param);
    	if($data){
    		return json(['message'=>'发布成功!']);
    	}
    	return json(['message'=>$jobs->getError()]);
    }
}
